import numpy as np
import sys
import re
sys.path.append(".")

datasets = {
    "./asset/dev.txt": r"[\u4e00-\u9fa5]+", 
    "./asset/train.txt": r"[\u4e00-\u9fa5]+", 
    "./asset/words.txt": r"[\u4e00-\u9fa5]+", 
    "./asset/places.txt": r"[\u4e00-\u9fa5]+", 
    "./asset/names.txt": r"[\u4e00-\u9fa5]+", 
    "./asset/famous.txt": r"[\u4e00-\u9fa5]+", 
}


class Dictionary(object):
    def __init__(self):
        self.data = []
        self.max_len = 0

    def load(self, datasets, maxlen):
        for dataset, pattern in datasets.items():
            print("Loading dataset from {}.".format(dataset))
            with open(dataset) as F:
                sentences = F.readlines()
            for sentence in sentences:
                words = re.findall(pattern, sentence)
                for word in words:
                    length = len(word)
                    if length > maxlen:
                        continue
                    if length >= self.max_len:
                        self.expand_len(length)
                    self.data[length].append(word)
        
        for _, l in enumerate(self.data):
            self.data[_] = set(l)

    def match(self, sentence):
        ret = dict()
        ret[1] = sentence[0]
        m = min(self.max_len, len(sentence)+1)
        for l in range(1, m):
            word = sentence[0:l]
            if word in self.data[l]:
                ret[l] = word
        return ret

    def expand_len(self, length):
        assert length >= self.max_len
        while length >= self.max_len:
            self.data.append([])
            self.max_len += 1

    def __str__(self):
        for i in range(self.max_len):
            tmp = list(self.data[i])
            if len(tmp) > 20:
                tmp = tmp[:20]
            print("{}: {}".format(i, " ".join(tmp)))
        return ""
    
    def __len__(self):
        ret = 0
        for i in range(self.max_len):
            ret += len(self.data[i])
        return ret

class Morpheme(object):
    def __init__(self):
        # self.data = dict()
        self.data = dict()
        self.m = dict()
        self.s = dict()
        
    # def load(self, datasets):
    #     for dataset, pattern in datasets.items():
    #         print("Loading datasets from {}...".format(dataset))
    #         with open(dataset) as F:
    #             sentences = F.readlines()
    #         for sentence in sentences:
    #             words = re.findall(pattern, sentence)
    #             for word in words:
    #                 length = len(word)
    #                 if length != 1:
    #                     continue
    #                 self.data[word] = self.data.get(word, 0) + 1
    #     print("Morpheme loaded.")
    
    def load(self, datasets):
        for dataset, pattern in datasets.items():
            print("Loading datasets from {}...".format(dataset))
            with open(dataset) as F:
                sentences = F.readlines()
            for sentence in sentences:
                words = re.findall(pattern, sentence)
                for word in words:
                    for char in word:
                        self.m[char] = self.m.get(char, 0) + 1
                    if len(word) == 1:
                        self.s[word] = self.s.get(word, 0) + 1

        for char, value in self.m.items():
            self.data[char] = self.s.get(char, 0)/value

    def get(self, char):
        if len(char) != 1:
            return 0
        else:
            return self.data.get(char, 0)

class Name(object):
    def __init__(self):
        self.data = set()
    
    def load(self, datasets):
        for dataset, pattern in datasets.items():
            print("Loading datasets from {}...".format(dataset))
            with open(dataset) as F:
                sentences = F.readlines()
            for sentence in sentences:
                words = re.findall(pattern, sentence)
                for word in words:
                    self.data.add(word)
        print("Name dict loaded.")


# if __name__ == "__main__":
#     m = Morpheme()

